<?php
/**
 * 获取当前URL所在的目录
 */
$top_menu or $top_menu = '';
$menu or $menu = array();
$url or $url = Core::url()->site( Request::$uri );

function get_sub_menu( $admin_menu , $url , & $found=-1 )
{
    $menu = array();
    $sub_menu = false;
    foreach ($admin_menu as $k=>$v)
    {
        if ( is_array($v) )
        {
            if ( isset($v['href']) && $v['href']==$url )
            {
                # 如果当前URL和$v['href']的设置完全相同，则返回
                $menu = array($k);
                $found = true;
                break;
            }
            else
            {
                $url_len = $v['href']?strlen($v['href']):0;
                if( (!isset($v['href']) && null===$url) || (isset($v['href']) && substr($url,0,$url_len)==$v['href']) )
                {
                    # 如果当前URL和$v['href']的前部分相同，则记录下来
                    if ( $url_len>$found )
                    {
                        $found = $url_len;
                        $sub_menu = array($k);
                    }
                }

                $submenu = get_sub_menu( $v, $url,$found );
                if ( $submenu )
                {
                    if ( true===$found )
                    {
                        $menu = array($k);
                        $menu = array_merge($menu,$submenu);
                        break;
                    }
                    else
                    {
                        $sub_menu = array_merge(array($k),$submenu);
                    }
                }
            }
        }
    }
    if ( $menu )
    {
        return $menu;
    }
    elseif( $sub_menu )
    {
        return $sub_menu;
    }
    else
    {
        return false;
    }
}
function check_perm( & $admin_menu)
{
    $perm = Session::instance()->member()->perm();
    $havearr = false;
    foreach ( $admin_menu as $k=>&$v )
    {
        if ( is_array($v) )
        {
            if (isset($v['perm']))
            {
                $perm_key = $v['perm'];
                unset($v['perm']);
                if ( false!==strpos($perm_key,'||') )
                {
                    $perm_key = explode('||', $perm_key);
                    $have_perm = false;
                    foreach ($perm_key as $p)
                    {
                        if ( $perm->is_own($p) )
                        {
                            $have_perm = true;
                            continue;
                        }
                    }
                    if (!$have_perm)
                    {
                        unset($admin_menu[$k]);
                        continue;
                    }
                }
                elseif ( false!==strpos($perm_key,'&&') )
                {
                    $perm_key = explode('&&', $perm_key);
                    foreach ($perm_key as $p)
                    {
                        if ( !$perm->is_own($p) )
                        {
                            unset($admin_menu[$k]);
                            continue 2;
                        }
                    }
                }
                else
                {
                    # 检查权限
                    if ( !$perm->is_own($perm_key) )
                    {
                        unset($admin_menu[$k]);
                        continue;
                    }
                }
            }
            if ( false===check_perm( $v ) )
            {
                unset($admin_menu[$k]);
            }
            else
            {
                $havearr = true;
            }
        }
        elseif ( $k=='href' )
        {
            if ( $v !='#' && !preg_match('#^[a-z0-9]+://.*$#', $v) )
            {
                $v = (string)Core::url($v);
            }
        }

    }
    if ( false==$havearr && (!isset($admin_menu['href']) || $admin_menu['href']=='#' ) )
    {
        return false;
    }
}
check_perm($admin_menu);
if ( !$menu )
{
    $menu = get_sub_menu($admin_menu,$url);
}
if ( !$menu )
{
    # 如果还是没有，则获取首页面
    $tmp_default = current($admin_menu);
    $menu = get_sub_menu($admin_menu,$tmp_default['href']);
    if (!$page_title)$page_title = '管理首页';
}
if ( !$menu ) $menu = array();
$top_menu = current($menu);

if (!$location || !is_array($location))
{
    $location = array();
}

$this_key_len = count($menu) + count($location);

if ( $page_title )
{
    $location[] = $page_title;
    $this_key_len += 1;
}
elseif( $location )
{
    end($location);
    $tmp_menu = current($location);
    $page_title = is_array($tmp_menu)?$tmp_menu['innerHTML']:(string)$tmp_menu;
}
else
{
    $i=0;
    $tmp_menu = $admin_menu;
    foreach ($menu as $key){
        $i++;
        $tmp_menu = $tmp_menu[$key];
        if ($i==$this_key_len)
        {
            # 获取标题
            $page_title = strip_tags($tmp_menu['innerHTML'],'');
        }
    }
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="zh-cn" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title;?></title>
<script type="text/javascript">
var MyQEE = {};
MyQEE.Url = {
    Site : '<?php echo rtrim(Core::url()->site(''),'/');?>'
};
</script>
<script type="text/javascript" src="<?php echo Core::url()->site('statics/js/global.js');?>"></script>
<script type="text/javascript" src="<?php echo Core::url()->site('statics/js/ymPrompt/ymPrompt.js');?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo Core::url()->site('statics/js/ymPrompt/skin/simple/ymPrompt.css');?>" />
<link rel="stylesheet" type="text/css" href="<?php echo Core::url()->site('statics/css/global.css');?>" />
</head>
<body>
<iframe width="1" height="1" name="hiddenFrame" id="hiddenFrame" style="display:none;"></iframe>
<!--
header - begin
-->
<div id="div_header">
    <div id="topdiv">
        <div class="topbg">
            <div class="mainWidth clear">
                <div id="logo">
                    <?php $logurl = Core::url()->site('statics/images/logo.png');?>
                    <img src="<?php echo $logurl;?>" style="_display:none;" />
                    <img src="<?php echo  Core::url()->site('statics/images/spacer.gif');?>" style="display:none;_display:inline;_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=image,src=<?php echo $logurl;?> );" alt="Logo" />
                </div>
                <div id="top_info_div">
                    <?php
                    $username = Session::instance()->member()->username;
                    if ($username)
                    {
                    ?>
                    <font style="color:#666;">欢迎您：<?php echo $username;?></font>
                    <span id="user_link_div">
                    <?php
                    $menu_arr = Core::config('admin/top_menu');
                    if ($menu_arr)foreach ( $menu_arr as $item)
                    {
                        if (!isset($item['innerHTML']))continue;
                        if ( isset($item['href']) )
                        {
                            echo ' | <a href="'.Core::url()->site($item['href']).'">'.$item['innerHTML'].'</a>';
                        }
                        else
                        {
                            echo ' | <span style="color:#666;">' . $item['innerHTML'] .'</span>';
                        }
                    }
                    ?>
                    </span> | <a href="<?php echo Core::url()->site('login/out');?>">退出</a> |
                    <?php
                    }
                    ?>
                    <a href="<?php echo Core::url('/');?>">管理首页</a>
                </div>
            </div>
        </div>
        <div id="banner">
            <div class="mainWidth clear">
                <ul class="ul" id="menu_ul">
                <?php
                if ($admin_menu)foreach ($admin_menu as $k=>&$v){
                    if (is_array($v)){
                        if (isset($v['innerHTML'])){
                            echo '<li><a onclick="return change_menu(\''.$k.'\',this);"'.($k=='base1'?' class="hover"':'');
                            if ($k==$top_menu)echo ' class="hover"';
                            foreach ($v as $key=>$value){
                                if ($key=='innerHTML' || $key=='perm' || $key=='icon')
                                {
                                    continue;
                                }
                                elseif (is_string($value))
                                {
                                    echo ' '.$key.'="'.$value.'"';
                                }
                                elseif (!isset($v['href']) && is_array($value) && isset($value['href']))
                                {
                                    # 没有链接的话，取子菜单第一个
                                    echo ' href="'.$value['href'].'"';
                                    $v['href'] = $value['href'];
                                }
                            }
                            if (!isset($v['href'])) {
                                echo ' href="#"';
                            }
                            if ( isset($v['icon']) )
                            {
                                $html = '<img src="'.Core::url('statics/images/'.$v['icon']).'" />' .$v['innerHTML'];
                            }
                            else
                            {
                                $html = $v['innerHTML'];
                            }
                            echo '><strong>'.$html.'</strong></a></li>';
                        }
                    }
                }

                ?>
                </ul>
            </div>
        </div>
    </div>
    <div id="menutagdiv">
        <div class="menutagdiv">
            <ul class="menutagul ul">
                <li style="float:right;width:64px;padding-right:5px;">
                    <div id="go_and_back_button"><a id="button_back" href="#" onclick="window.history.back();return false;" title="转到上一页"></a><a id="button_renew" href="#" onclick="window.location.reload();return false;" title="刷新本页"></a><a href="#" id="button_go" onclick="window.history.forward();return false;" title="转到下一页"></a></div>
                </li>
                <?php
                if ($quick_menu){
                ?>
                <li style="float:right;padding-right:5px;">
                    <div id="menutag_more">
                        <style type="text/css">
                            #menutag_more:hover{height:<?php echo 18*count($quick_menu);?>px;}
                        </style>
                        <ul class="menutag_more ul">
                            <?php
                            foreach ($quick_menu as $k=>$item)
                            {
                                echo '<li><a href="'.Core::url($k).'">' .$item. '</a></li>';
                            }
                            ?>
                        </ul>
                    </div>
                </li>
                <?php
                }
                ?>
                <li class="tag_menu"><font><strong><a href="<?php echo Core::url()->site('/');?>">网站管理</a></strong></font></li>
<?php
$i=0;
$tmp_menu = $admin_menu;
foreach ($menu as $key){
    $i++;
    $tmp_menu = $tmp_menu[$key];
    if ($i==$this_key_len || !isset($tmp_menu['href']) ){
        echo '<li class="tag_menu hover"><font><strong style="cursor:default;">'.$tmp_menu['innerHTML'].'</strong></font></li>';
    }else{
        echo '<li class="tag_menu"><font><strong><a href="'.$tmp_menu['href'].'">'.$tmp_menu['innerHTML'].'</a></strong></font></li>';
    }
}

$i=0;
$location_count = count($location);
if ($location)foreach ($location as $value){
    $i++;
    if ( is_array($value) )
    {
        $tag = isset($value['href'])?'a':'span';
    }
    else
    {
        $tag = 'span';
    }
    echo "\r\n".'<li'.($location_count==$i?' class="tag_menu hover"':'tag_menu').'><font><strong'.($tag=='span'?' style="cursor:default;"':'').'>';

    if ( is_array($value) )
    {
        echo '<'.$tag.' ';
        foreach ($value as $k=>$v)
        {
            if ($k=='innerHTML')continue;
            echo ' ' . $k.'="'.$v.'"';
        }
        echo '>'.$value['innerHTML'].'</'.$tag.'>';
    }
    else
    {
        echo (string)$value;
    }

    echo '</strong></font></li>';
}
;?>
            </ul>
        </div>
    </div>
</div>
<script type="text/javascript">
//更新前进后退菜单
setTimeout(function(){
    var obj_go = MyQEE.$('button_go');
    var obj_back = MyQEE.$('button_back');
    if (!document.referrer)
    {
        obj_back.style.opacity=0.3;
        obj_back.style.filter='alpha(opacity=30)';
    }
},10);
</script>
<!--
header - end
-->
<!--
leftmenu - begin
-->
<div id="leftmenudiv">
<!--    <div id="leftmenubar" title="收起左侧"></div>-->
    <div id="leftmenu">
<!--        <div id="leftmenutitle"><span><font id="leftmenutext"></font></span></div>-->
        <div>
        <ul id="leftmenulink" class="ul"></ul>
        </div>
    </div>
</div>

<script type="text/javascript">
function show_or_hidden_menu(obj_id)
{
    var obj = MyQEE.$('_left_menu'+obj_id);
    if (!obj)return false;
    var setdata = function()
    {
        var data = MyQEE.localStorage.getItem('left_menu_fav') || ',';
        if (obj.style.display=='none')
        {
            data += obj_id + ',';
        }
        else
        {
            var re = new RegExp (','+obj_id+',','g');
            data = data.replace(re,',');
        }
        if (data==',')
        {
            MyQEE.localStorage.removeItem('left_menu_fav');
        }
        else
        {
            MyQEE.localStorage.setItem('left_menu_fav',data);
        }
    }
    MyQEE.resizeDiv(obj,setdata);
}
function change_menu(menu_key,a_obj,show_key)
{
    var menu = <?php echo json_encode($admin_menu);?>;
    var show_key = <?php echo json_encode($menu)?>;
    var obj = MyQEE.$('leftmenulink');
    var thismenu = menu[menu_key];
    if (!thismenu)return false;
    if (a_obj){
        var url = document.location.href.replace(/\#.*/,'');
        //存在链接，则直接页面跳转
        if (a_obj.href){
            if (a_obj.href==url+'#'){
                //空链接
            }else if (a_obj.href.replace(/\#.*/,'')==url){
                //相同链接，或略
            }else{
                //返回true，允许页面跳转到href指定url上
                return true;
            }
        }
        //已在当前菜单上，或略点击
        if (a_obj.className=='hover')return false;
    }

    var new_title = '';

    var show_key_len = 0;
    if (show_key)show_key_len = show_key.length;


    var show_html = function(keystr,arr,n,islast,leftstr,isfocus)
    {
        leftstr = leftstr||'';
        n=n||0;
        if(n==0)isfocus=show_key[0]==menu_key?true:false;
        var tmphtml = '';
        var tmpli = '';
        var tmparr = '';
        var len = 0;
        for (var k in arr){
            if (typeof arr[k] =='object'){
                len++;
            }
        }

        var i=0;
        for (var k in arr){
            if (typeof arr[k] =='object'){
                i++;
                tmpli += show_html(
                    keystr+'_'+k,
                    arr[k],
                    n+1,
                    i==len?true:false ,
                    n>1?(leftstr+(islast?'0':'3')):'' ,
                    isfocus?(k==show_key[n+1]?true:false):false
                );
            }else if (k!='innerHTML'){
                tmparr += ' '+k+'="'+arr[k]+'"';
            }
        }
        if (arr['innerHTML'])
        {
            if (typeof arr['title'] == 'undefined')
            {
                tmparr += ' title="'+arr['innerHTML']+'"';
            }
            if (n==0){
                new_title = arr['innerHTML'];
            }else{
                var tagleft = '';
                var tagright = '';
                for(var i=0;i<leftstr.length;i++)
                {
                    tagleft += '<div class="menu_tree_'+leftstr.substr(i,1)+'">';
                    tagright += '</div>';
                }
                if (arr['href']){
                    tmparr = '<a'+tmparr+'>'+arr['innerHTML']+'</a>';
                }else{
                    tmparr = '<font'+tmparr+'>'+arr['innerHTML']+'</font>';
                }
                tmparr = tagleft + '<div class="menu_tree_'+(n==1?'title':(islast?'2':'1'))+'">' + tmparr + '</div>' + tagright;
                tmphtml += '<li'
                    +' class="menu_tree_li'
                    +(tmpli?' menu_tree_btn':'')
                    +(n+1==show_key_len && isfocus?' hover"':'')
                    +'">'
                    +(tmpli?'<div class="show_hidden_button" title="展开/收缩子菜单" onclick="show_or_hidden_menu(\''+keystr+'\')"></div>':'')
                    +tmparr
                    +'</li>';
            }
        }
        if (tmpli){
            if (n>0)
            {
                var left_menu_fav = MyQEE.localStorage.getItem('left_menu_fav')||'';
                tmphtml += '<ul'+(left_menu_fav.indexOf(','+keystr+',')!=-1?' style="display:none;"':'')+' class="ul menu_tree_block" id="_left_menu'+keystr+'">'+tmpli+'</ul>';
            }
            else
            {
                tmphtml += tmpli;
            }
        }

        return tmphtml;
    }

    var html = show_html('',thismenu);

    if (html){
        obj.innerHTML = html;
        if (a_obj){
            var all_a = MyQEE.$('menu_ul').getElementsByTagName('a');
            for(var i=0;i<all_a.length;i++){
                all_a[i].className = '';
            }
            a_obj.className = 'hover';
        }
        return false;
    }else{
        return true;
    }
}
change_menu('<?php echo $top_menu;?>',null);
</script>
<!--
leftmenu - end
-->
<div id="maindiv_leftline"></div>
<div id="maindiv_rightline"></div>

<script type="text/javascript">
if (MyQEE.isinFrame()){
    //若页面在框架内，则隐藏多余的显示内容
    MyQEE.$('div_header').style.display='none';
    MyQEE.$('leftmenudiv').style.display='none';
    MyQEE.$('maindiv_leftline').style.display='none';
    MyQEE.$('maindiv_rightline').style.display='none';
    if (parent.MyQEE.$('menutagdiv')) parent.MyQEE.$('menutagdiv').innerHTML = MyQEE.$('menutagdiv').innerHTML;
}else{
    window.onscroll = function()
    {
        if (MyQEE.is_ie && MyQEE.ie<7)return;
        var obj_div = MyQEE.$('toolbar_div');
        var obj = MyQEE.$('toolbar_table');
        if (!obj_div || !obj)return;
        var dom = MyQEE.DOM();
        var main_height = MyQEE.$('maindiv').offsetHeight;
        var body_height = dom.clientHeight;
        var hh = dom.scrollHeight - (body_height+(dom.scrollTop||document.body.scrollTop)) - MyQEE.$('bottomdiv').offsetHeight - 9;
        if (body_height-main_height<101 && hh>0)
        {
            obj.style.position ='fixed';
            obj.style.bottom ='0';
            obj.style.width = obj_div.offsetWidth+'px';
            obj_div.style.height = obj.offsetHeight+'px';
        }
        else
        {
            obj.style.width = '';
            obj.style.position ='';
            obj.style.bottom ='';
        }
    }

    window.onresize = function(){
        var h = MyQEE.DOM().clientHeight;
        MyQEE.$('leftmenu').style.height =
        MyQEE.$('maindiv_leftline').style.height =
        MyQEE.$('maindiv_rightline').style.height = Math.max(h-86,300)+'px';
        MyQEE.$('maindiv').style.minHeight = (MyQEE.DOM().clientHeight - 127) + 'px';

        window.onscroll();
    }
}
</script>


<div class="clear" style="height:0;overflow:hidden;"></div>

<div id="maindiv">